package com.banciyuan.bcywebview.base.view.textview;


import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.Layout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;

import com.banciyuan.bcywebview.R;

public class ExpandedTextView extends AppCompatTextView {

    private boolean canExpanded = true;
    private Context mContext;
    private SpannableString charSequence;
    private View.OnClickListener onClickListener;
    private OnProcessClick onProcessClick;
    private int res_id;
    private int lines;

    public ExpandedTextView(Context context) {
        super(context);
        mContext = context;
    }

    public ExpandedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public ExpandedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    private void resetText() {
        Layout layout = getLayout();
        if (null == layout)
            return;
        CharSequence text = getText();
        if (layout.getLineCount() > lines) {
            int lineEndPos = layout.getLineVisibleEnd(lines - 1);
            if (lineEndPos < text.length()) {
                if (text.length() > lineEndPos - 7) {
                    CharSequence oldStr = text.subSequence(0, lineEndPos - 7);
                    text = oldStr.toString().replaceAll("\\n", "") + mContext.getString(res_id);
                } else {
                    text = text.toString() + mContext.getString(res_id);
                }
            }
            SpannableString sps = onProcessClick.onProcessSucc(text);
            if (canExpanded) {
                ExpandedClickSpan expandedClickSpan = new ExpandedClickSpan();
                if (sps.length() > 5) {
                    sps.setSpan(expandedClickSpan, sps.length() - 5, sps.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                setText(sps);
            } else {
                setText(sps);
            }
        } else {
            setText(charSequence);
        }
        setMovementMethod(LinkMovementMethod.getInstance());
    }

    public void setText(SpannableString text, boolean canExpanded, OnProcessClick onProcessClick, View.OnClickListener onClickListener, int res_id, int lines) {
        this.canExpanded = canExpanded;
        this.charSequence = new SpannableString(text);
        this.onClickListener = onClickListener;
        this.onProcessClick = onProcessClick;
        this.res_id = res_id;
        this.lines = lines;
        setText(text);
        post(new Runnable() {
            @Override
            public void run() {
                resetText();
            }
        });
    }

    private class ExpandedClickSpan extends ClickableSpan {

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(mContext.getResources().getColor(R.color.timeline_green));
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(View view) {
            setText(charSequence);
            scrollTo(0, 0);
            setMovementMethod(LinkMovementMethod.getInstance());
            onClickListener.onClick(view);
        }
    }

    public interface OnProcessClick {
        SpannableString onProcessSucc(CharSequence text);
    }

}
